using AnalyStock.DataManage;
namespace AnalyStock.GrapImage;
/// <summary>
///     监视股的当天价格变化浏览面板
/// </summary>
public partial class ChartKline : UserControl
{
    private System.Timers.Timer _timer;
    private SynchronizationContext _syncContext;
    private void InitializePanel_KeyStocksPrice()
    {
        KeyStocksPricePanel.Visibility = Visibility.Hidden;
        KeyStocksPricePanel.InitializeMouseDarg(this);
        _syncContext = SynchronizationContext.Current ?? new SynchronizationContext();
        _timer = new System.Timers.Timer(30 * 1000);
        _timer.Elapsed += new System.Timers.ElapsedEventHandler(PostKeyStockPrice);
        _timer.AutoReset = true;

    }
    private async Task<IList<DailyOfTencent>> GetCurrentPriceAsync()
    {
        var items = new List<DailyOfTencent>();
        foreach (var d in SelectStocks.Where(n => n.Conception.Equals("监视股")))
        {
            var _current = await FillCurrentDailyAsync(d.Ts_code).ConfigureAwait(false);
            if (_current is not null)
            {
                items.Add(_current);
            }
        }
        return items;
    }

    private async void PostKeyStockPrice(object sender, System.Timers.ElapsedEventArgs e)
    {
        //传递参数
        SendOrPostCallback callback = (object e) =>
        {
            TextPriceTable((IList<DailyOfTencent>)e);
        };
        _syncContext.Post(callback,
            await GetCurrentPriceAsync().ConfigureAwait(false));
    }

    private void TextPriceTable(IList<DailyOfTencent> dailes)
    {
        DGKeyStocksPrice.ItemsSource = dailes;
    }
    private void KeyStocksPrice_Click(object sender, RoutedEventArgs e)
    {
        if (!_timer.Enabled)
        {
            //_timer.Enabled = true;
            _timer.Start();
            PostKeyStockPrice(null, null);
            KeyStocksPricePanel.Visibility = Visibility.Visible;
        }
        else
        {
            // _timer.Enabled = false;
            _timer.Stop();
            KeyStocksPricePanel.Visibility = Visibility.Hidden;
        }

    }
}

public class Pct_chg_BrushConverter : System.Windows.Data.IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        return (float)value switch
        {
            <= 0 => new SolidColorBrush(Colors.SeaGreen),
            > 0 and <= 9.0f => new SolidColorBrush(Colors.DarkRed),
            > 9.0f => new SolidColorBrush(Colors.IndianRed),
            _ => new SolidColorBrush(Colors.Transparent)
        };

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

public class TurnOver_BrushConverter : System.Windows.Data.IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        return (float)value switch
        {
            > 10 and <= 15 => new SolidColorBrush(Colors.Sienna),
            > 15 and <= 25 => new SolidColorBrush(Colors.DarkRed),
            > 25 => new SolidColorBrush(Colors.IndianRed),
            _ => new SolidColorBrush(Colors.Transparent)
        };
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}